import logging
import numpy as np
from qtorch.core.qstrategy import QStrategy

class SimpleSMAStrategy(QStrategy):
    """
    简单双均线交叉策略 - 使用新的QStrategy API实现
    
    参数：
        fast_period: 快线周期
        slow_period: 慢线周期
    """
    
    def __init__(self, fast_period=5, slow_period=20):
        super().__init__()
        self.fast_period = fast_period
        self.slow_period = slow_period
        self.fast_ma = None
        self.slow_ma = None
        
    def initialize(self):
        """策略初始化"""
        logging.info(f"初始化双均线策略: 快线={self.fast_period}, 慢线={self.slow_period}")
        
    def next(self):
        """
        处理当前数据点，生成交易信号
        
        返回:
            int: 1(买入)、0(持有)、-1(卖出)
        """
        # 计算移动平均线
        self.fast_ma = self.SMA(self.fast_period)
        self.slow_ma = self.SMA(self.slow_period)
        
        # 如果数据不足，等待
        if self.fast_ma is None or self.slow_ma is None:
            return 0
            
        # 金叉：快线上穿慢线
        if self.fast_ma > self.slow_ma and self.position <= 0:
            return 1  # 买入信号
            
        # 死叉：快线下穿慢线
        elif self.fast_ma < self.slow_ma and self.position >= 0:
            return -1  # 卖出信号
            
        # 其他情况，持仓不变
        return 0